﻿using MediatR;
using Messhall.Domain.Permission.BusinessObjects;

namespace Messhall.Application.Permission.Commands;

/// <summary>
/// 更新用户信息
/// </summary>
public class UpdateUsersCommandHandler : CommandHandlerBase, IRequestHandler<UpdateUsersCommand, bool>
{
    public async Task<bool> Handle(UpdateUsersCommand updateUsersCommand, CancellationToken cancellationToken)
    {
        var request = updateUsersCommand.Request;

        if (request.KeyId.Equals(default))
            throw new Exception($"用户Id为{Guid.Empty}");

        var user = await base.usersRepository.FindAsync(request.KeyId);

        if (user == null)
            throw new Exception("未查询到用户信息");

        user.SetAccountNo(request.AccountNo);
        user.SetIntegral(request.Integral);
        user.SetMobile(request.Mobile);
        user.SetMoney(request.Money);
        user.SetVip(request.IsVip);
        user.SetPassWord(request.PassWord);

        await appUnitOfWork.SaveChangesAsync();

        return true;
    }
}